<?php
/**
 * @package orinoco.framework
 * @subpackage application
 * @author Ryan Yonzon <rawswift@gmail.com>
 * @copyright Copyright 2008 Ryan Yonzon 
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * autoload classes; magic method
 *
 * @param string $_class_name name of class to instantiate
 * @return true if class is found else trow an exception
 */
function __autoload($_class_name) {
	
	$_route = new Router();
	
	// try 1 - try model classes
	$_app_model_file = APPLICATION_DIR . $_route->_application . '/' . MODEL_DIR;
	$_model_path = $_app_model_file . strtolower($_class_name) . MODEL_FILE_SUFFIX . MODEL_FILE_EXTENSION;
		if(file_exists($_model_path)){
			require($_model_path);
			return true;
		}
		
	// try 2 - try extension classes
	$_exts_path = EXTS_DIR . strtolower($_class_name) . EXTS_FILE_SUFFIX . EXTS_FILE_EXTENSION;
		if(file_exists($_exts_path)){
			require($_exts_path);
			return true;
		}

	// try 3 - try adapter classes; needed by database class
	$_adapter_path = ADAPTER_DIR . strtolower($_class_name) . ADAPTER_FILE_SUFFIX . ADAPTER_FILE_EXTENSION;
		if(file_exists($_adapter_path)){
			require($_adapter_path);
			return true;
		}
		
	throw new Exception('Model or Extension class "' . $_class_name . '" does not exists.');
		
} // end __autoload()

require('view.class.php');

/**
 * handles application's controller and loads associated model
 */
class Controller extends View {

	// variables that will be available for the developer's controllers
	public $_application;
	public $_controller;
	public $_action;
	public $_id;
	public $_get;
	public $_post;
	
	/**
	 * constructor 
	 */
	public function __construct() {
	
		$_mapped_uri = new Router();

		// store application, controller, method/action,
		// requests (get/post) and trailing parameters into public variable
		$this->_application = $_mapped_uri->_application;
		$this->_controller = $_mapped_uri->_controller;
		$this->_action = $_mapped_uri->_method;
		
			if(isset($_mapped_uri->_id)) {
				$this->_id = $_mapped_uri->_id;
			}
		
			if(isset($_mapped_uri->_get)) {
				$this->_get = $_mapped_uri->_get;
			}

			if(isset($_mapped_uri->_post)) {
				$this->_post = $_mapped_uri->_post;
			}
			
		// check if we're gonna autoload a model
		if(AUTOLOAD_MODEL) {
			// automatically load the associated model 
			$_app_model_file = APPLICATION_DIR . $this->_application . '/' . MODEL_DIR . $this->_controller . MODEL_FILE_SUFFIX . MODEL_FILE_EXTENSION;
				// check if the associated model exists
				if(file_exists($_app_model_file)) {
					// load model file
					require($_app_model_file);
						// set model name; use controller name
						// ex. $this->example = new example()					
						$_model_name = $this->_controller;
						$_model_real_class_name = $this->_controller . MODEL_SUFFIX;
						// create model object
						$this->$_model_name = new $_model_real_class_name();
				}
		}

	} // end constructor method
	
	/**
	 * destructor
	 */
	public function __destruct() {
		unset($this);
	}
	
} // end class

// -EOF-